package com.fmdj.mis.api.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.fmdj.common.exception.GlobalException;
import com.fmdj.common.util.CommonResult;
import com.fmdj.common.util.PageUtils;
import com.fmdj.mis.api.controller.form.*;
import com.fmdj.mis.api.feign.*;
import com.fmdj.mis.api.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OdrServiceApi odrServiceApi;

    @Autowired
    private CstServiceApi cstServiceApi;

    @Autowired
    private DrServiceApi drServiceApi;

    @Autowired
    private MpsServiceApi mpsServiceApi;

    @Autowired
    private NebulaServiceApi nebulaServiceApi;
    @Override
    public PageUtils searchOrderByPage(SearchOrderByPageForm form) {
        CommonResult r = odrServiceApi.searchOrderByPage(form);
        // 可以把返回的结果强转成map再进行转换成pageutils
        HashMap hashMap = (HashMap)r.get(CommonResult.RETURN_RESULT);
        PageUtils pageUtils = BeanUtil.toBean(hashMap, PageUtils.class);
        return pageUtils;
    }

    @Override
    public HashMap searchOrderComprehensiveInfo(long orderId) {
        //建立一个map结果集存放所有信息
        HashMap map = new HashMap<>();
        
        //查询订单综合信息
        SearchOrderContentForm form_1 = new SearchOrderContentForm();
        form_1.setOrderId(orderId);
        CommonResult r = odrServiceApi.searchOrderContent(form_1);
        if (!r.containsKey(CommonResult.RETURN_RESULT)) {
            throw new GlobalException("查询订单相关信息时出错");
        }
        HashMap content = (HashMap) r.get(CommonResult.RETURN_RESULT);
        map.put("content", content);

        //查询顾客相关信息
        SearchCustomerBriefInfoForm form_2 = new SearchCustomerBriefInfoForm();
        form_2.setCustomerId(MapUtil.getLong(content, "customerId"));
        r = cstServiceApi.searchCustomerBriefInfo(form_2);
        HashMap customerInfo = (HashMap) r.get(CommonResult.RETURN_RESULT);
        map.put("customerInfo", customerInfo);

        //查询司机相关信息
        SearchDriverBriefInfoForm form_3 = new SearchDriverBriefInfoForm();
        form_3.setDriverId(MapUtil.getLong(content, "driverId"));
        r = drServiceApi.searchDriverBriefInfo(form_3);
        HashMap driverInfo = (HashMap) r.get(CommonResult.RETURN_RESULT);
        map.put("driverInfo", driverInfo);

        //查询地图相关参数信息
        CalculateDriveLineForm form_4 = new CalculateDriveLineForm();
        HashMap startPlaceLocation = (HashMap) content.get("startPlaceLocation");
        HashMap endPlaceLocation = (HashMap) content.get("endPlaceLocation");
        form_4.setStartPlaceLatitude(MapUtil.getStr(startPlaceLocation, "latitude"));
        form_4.setStartPlaceLongitude(MapUtil.getStr(startPlaceLocation, "longitude"));
        form_4.setEndPlaceLatitude(MapUtil.getStr(endPlaceLocation, "latitude"));
        form_4.setEndPlaceLongitude(MapUtil.getStr(endPlaceLocation, "longitude"));
        r = mpsServiceApi.calculateBestDrivingLine(form_4);
        HashMap driveLine = (HashMap) r.get(CommonResult.RETURN_RESULT);
        map.put("driveLine", driveLine); //from to key等参数

        //判断订单状态
        int status = (int) content.get("status");
        if (status>=5&&status<=8) {
            //该订单处于结束状态,需要查询司机的所有Gps信息
            SearchOrderGpsForm form_5 = new SearchOrderGpsForm();
            form_5.setOrderId(orderId);
            r = nebulaServiceApi.searchOrderGps(form_5);
            ArrayList<HashMap> orderGps = (ArrayList) r.get(CommonResult.RETURN_RESULT);
            map.put("orderGps", orderGps);
        }else {
            //该订单处于进行中状态,需要查询司机的最后一条(最新)Gps信息
            SearchOrderLastGpsForm form_5 = new SearchOrderLastGpsForm();
            form_5.setOrderId(orderId);
            r = nebulaServiceApi.searchOrderLastGps(form_5);
            HashMap lastGps = (HashMap) r.get(CommonResult.RETURN_RESULT);
            map.put("lastGps", lastGps);
        }
        //返回查询到的所有信息 订单信息 订单顾客信息 订单司机信息 订单地图信息 订单Gps信息(所有gps或者最后gps)
        return map;
    }


    @Override
    public HashMap searchOrderLastGps(SearchOrderLastGpsForm form) {
        //首先查询订单的状态 如果订单状态为4代表处于正在代驾的状态,那么取出最后一条Gps定位
        SearchOrderStatusForm statusForm = new SearchOrderStatusForm();
        statusForm.setOrderId(form.getOrderId());
        CommonResult r = odrServiceApi.searchOrderStatus(statusForm);
        if (!r.containsKey(CommonResult.RETURN_RESULT)) {
            throw new GlobalException("不存在该订单");
        }
        int status = (int) r.get(CommonResult.RETURN_RESULT);
        if (status==4) {
            r = nebulaServiceApi.searchOrderLastGps(form);
            HashMap hashMap = (HashMap) r.get(CommonResult.RETURN_RESULT);
            return hashMap;
        }
        return null;
    }

    @Override
    public ArrayList<HashMap> searchOrderStartLocationIn30Days() {
        //主要就是将查询结果集进行转换 {"latitude": "40.220001", "longitude": "116.240001"} -> {"latitude": 40.220001, "longitude": 116.240001,count:2}
        CommonResult commonResult = odrServiceApi.searchOrderStartLocationIn30Days();
        ArrayList<HashMap> list = (ArrayList<HashMap>) commonResult.get(CommonResult.RETURN_RESULT);
        //建立结果集
        ArrayList<HashMap> result = new ArrayList<>();
        //对相同坐标统计数量 利用CollectionUtil
        //获取到的形式例如: Map{{"latitude": "40.220001", "longitude": "116.240001"}=2,{"latitude": "40.220001", "longitude": "116.240001"}=4}
        Map<HashMap, Integer> map = CollectionUtil.countMap(list);
        map.forEach((key, value)->{
            //将key中的经纬度转换成double小数类型
            key.replace("latitude",MapUtil.getDouble(key, "latitude"));
            key.replace("longitude",MapUtil.getDouble(key, "longitude"));
            key.put("count",value);
            result.add(key);  //最后的形式为: {"latitude": 40.220001, "longitude": 116.240001,count:2}
        });
        return result;
    }
}
